import java.util.Arrays;

public class MyArrayList implements IList{
    private int DEFINE_SIZE = 3;
    private int usedSize;
    private int[] Array;
    public MyArrayList() {
       this.Array = new int[DEFINE_SIZE];
    }

    public boolean isFull() {
        return this.DEFINE_SIZE == this.usedSize;
    }
    public void checkCapacity() {
        if(isFull()) {
            System.out.println("数组已满，但现以扩容成原来的两倍！");
            Array = Arrays.copyOf(Array, DEFINE_SIZE * 2);
        }
    }
    @Override
    public void add(int data) {
        checkCapacity();
        this.Array[usedSize] = data;
        usedSize++;
    }

    public void isIllegal(int pos) throws IsPosIllegal{
        if(pos < 0 || pos > usedSize) {
            System.out.println("pos不合法");
            throw new IsPosIllegal("数组下标异常:" + pos);
        }
    }
    @Override
    public void add(int pos, int data) {
        try {
            isIllegal(pos);
        }catch(IsPosIllegal e) {
            e.printStackTrace();
            return;
        }
        checkCapacity();
        for(int i = usedSize - 1; i >= pos; i--) {
            Array[i+1] = Array[i];
        }
        Array[pos] = data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        for(int i = 0; i < usedSize; i++) {
            if(Array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0; i < usedSize; i++) {
            if(Array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    public void isIllegalOnGetAndSet(int pos) throws IsPosIllegal{
        if (pos < 0 || pos >= usedSize) {
            System.out.println("pos不合法");
            throw new IsPosIllegal("该位置下标异常：" + pos);
        }
    }
    @Override
    public int get(int pos) {
        try {
            isIllegalOnGetAndSet(pos);
        }catch(IsPosIllegal e) {
            e.printStackTrace();
        }
        return Array[pos];
    }

    @Override
    public void set(int pos, int value) {
        try {
            isIllegalOnGetAndSet(pos);
        }catch(IsPosIllegal e) {
            e.printStackTrace();
            return;
        }
        Array[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int pos = 0;
        for(int i = 0; i < usedSize; i++) {
            if(Array[i] == toRemove) {
                pos = i;
            }
        }
        for(int i = pos; i < usedSize - 1; i++) {
            Array[i] = Array[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public int capacity() {
        return Array.length;
    }
    @Override
    public void clear() {
        usedSize = 0;
        System.out.println("已清空！");
    }

    @Override
    public void display() {
        for(int i = 0; i < usedSize; i++) {
            System.out.print(this.Array[i] + " ");
        }
        System.out.println();
    }
}
